home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / QuickDraw3D 1.6 SDK / Mac SampleCode Previous / Grab Bag Samples - Mac / Unsupported Libraries / PictRead.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  5.1 KB  |  228 lines  |  [TEXT/MPS ]

  1. /******************************************************************************
  2.  **                                                                             **
  3.  **     Module:        PictRead.c                                                 **
  4.  **                                                                          **
  5.  **                                                                          **
  6.  **     Purpose:                                                               **
  7.  **                                                                          **
  8.  **                                                                          **
  9.  **                                                                          **
  10.  **     Copyright (C) 1992-1995 Apple Computer, Inc.  All rights reserved.     **
  11.  **                                                                          **
  12.  **                                                                          **
  13.  *****************************************************************************/
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. #include <StandardFile.h>
  19. #include <Memory.h>
  20.  
  21. #include <QDOffscreen.h>
  22. #include <ImageCompression.h>
  23. #include "QD3D.h"
  24. #include "QD3DStorage.h"
  25. #include "PictRead.h"
  26.  
  27.  
  28. /*===========================================================================*\
  29.  *
  30.  *    Routine:    OpenPICTFile()
  31.  *
  32.  *    Comments:    open a PICT file and read it into a PICT handle
  33.  *
  34. \*===========================================================================*/
  35.  
  36. PicHandle OpenPICTFile(
  37.     FSSpecPtr fileSpec)
  38. {
  39.     short        fRefNum;
  40.     OSErr        err;
  41.     long        curEOF;
  42.     PicHandle    myPic;
  43.     long         count;
  44.     Ptr         buffer;
  45.     
  46.     /* open PICT file */
  47.     err = FSpOpenDF(fileSpec, fsCurPerm, &fRefNum);
  48.     if (err != 0) {
  49.         /*printf("Error - cannot open file\n"); */
  50.         exit(-1);
  51.     }
  52.     
  53.     /* get size of file */
  54.     err = GetEOF(fRefNum, &curEOF);
  55.     if (err != 0) {
  56.         /*printf("Error - cannot get EOF\n"); */
  57.         exit(-2);
  58.     }
  59.     
  60.     /* move the file mark to 512 */
  61.     err = SetFPos(fRefNum, fsFromStart, 512L);
  62.     if (err != 0) {
  63.         /*printf("Error - cannot seek 512\n"); */
  64.         exit(-3);
  65.     }
  66.  
  67.     /* size of data to read */
  68.     count = curEOF - 512;
  69.     
  70.     /* create the PicHandle */
  71.     myPic = (PicHandle)NewHandle(count);
  72.     HLock((Handle)myPic);
  73.     
  74.     /* read the PICT info */
  75.     buffer = (Ptr)(*myPic);
  76.     err = FSRead(fRefNum, &count, buffer);
  77.     if (err != 0) {
  78.         /*printf("Error - cannot read\n");*/
  79.         exit(-4);
  80.     }
  81.     HUnlock((Handle)myPic);
  82.     
  83.     /* release the file */
  84.     err = FSClose(fRefNum);
  85.     if (err != 0) {
  86.         /*printf("Error - cannot close file \n"); */
  87.         exit(-5);
  88.     }
  89.     
  90.     return (myPic);
  91. }
  92.  
  93.  
  94. /*===========================================================================*\
  95.  *
  96.  *    Routine:    GetPICTFile()
  97.  *
  98.  *    Comments:    Query user for PICT File
  99.  *
  100. \*===========================================================================*/
  101.  
  102. PicHandle GetPICTFile (
  103.     void)
  104. {
  105.     PicHandle             picHandle;
  106. //    typedef const OSType *ConstSFTypeListPtr;
  107.  
  108.     static SFTypeList    myTypes = { 'PICT', 0L } ;
  109.     StandardFileReply    mySFReply ;
  110.         
  111.     StandardGetFilePreview(nil, 1, myTypes, &mySFReply ) ;
  112.     
  113.     if (mySFReply.sfGood) {
  114.         picHandle = OpenPICTFile(&mySFReply.sfFile);
  115.         return (picHandle);
  116.     }
  117.     
  118.     return (0);
  119. }
  120.  
  121.  
  122. /*===========================================================================*\
  123.  *
  124.  *    Routine:    LoadMapPICT()
  125.  *
  126.  *    Comments:    take a PICT handle and loads it into a bitmap structure
  127.  *
  128. \*===========================================================================*/
  129.  
  130. short LoadMapPICT(
  131.     PicHandle             pict,
  132.     unsigned long         mapID,
  133.     unsigned long         mapSizeX,
  134.     unsigned long         mapSizeY,
  135.     TQ3StoragePixmap     *bMap)
  136. {
  137.     unsigned long             *textureMap;
  138.     unsigned long            *textureMapAddr;
  139.     unsigned long             *pictMap;
  140.     unsigned long            pictMapAddr;
  141.     register unsigned long     row;
  142.     register unsigned long     col;
  143.     Rect                     rectGW;
  144.     GWorldPtr                 pGWorld;
  145.     PixMapHandle             hPixMap;
  146.     unsigned long             pictRowBytes;
  147.     QDErr                    err;
  148.     GDHandle                oldGD;
  149.     GWorldPtr                oldGW;
  150.     short                    success;
  151.     
  152.     mapID;        /* unused argument */
  153.  
  154.     textureMapAddr = NULL;
  155.     pGWorld = NULL;
  156.  
  157.     /* save current port */
  158.     GetGWorld(&oldGW, &oldGD);
  159.  
  160.     /* create the GWorld */
  161.     SetRect(&rectGW, 0, 0, (unsigned short)mapSizeX, (unsigned short)mapSizeY);
  162.  
  163.     err = NewGWorld(&pGWorld, 32, &rectGW, 0, 0, useTempMem);
  164.     if (err != noErr)
  165.         return 0;
  166.  
  167.     success = 1;
  168.     
  169.     hPixMap = GetGWorldPixMap(pGWorld);
  170.     pictMapAddr = (unsigned long)GetPixBaseAddr (hPixMap);
  171.     pictRowBytes = (unsigned long)(**hPixMap).rowBytes & 0x3fff;
  172.     
  173.     /* put the PICT into the window */
  174.     SetGWorld(pGWorld, nil);
  175.     
  176.     LockPixels(hPixMap);
  177.     EraseRect(&rectGW);
  178.     DrawPicture(pict, &rectGW);
  179.         
  180.     /* allocate an area of memory for the texture */
  181.     textureMap = (unsigned long *)malloc(mapSizeX * mapSizeY * sizeof(unsigned long));
  182.     if (textureMap == NULL) {
  183.         success = 0;
  184.         goto bail;
  185.     }
  186.     /* bMap->image = (char *)textureMap; */
  187.  
  188.     /* copy the PICT into the texture */
  189.     textureMapAddr = textureMap;
  190.     for (row = 0L; row < mapSizeY; row++) {
  191.         pictMap = (unsigned long *)(pictMapAddr + (pictRowBytes * row));
  192.         for (col = 0L; col < mapSizeX; col++) {
  193.             *textureMap++ = (*pictMap++ | 0xff000000L);
  194.         }
  195.     }
  196.         
  197.     bMap->image = Q3MemoryStorage_New((const unsigned char *)textureMapAddr, 
  198.                                   mapSizeX * mapSizeY * sizeof(unsigned long));
  199.                                   
  200.     if (bMap->image == NULL) {
  201.         /* error */
  202.         success = 0;
  203.         goto bail;
  204.     }
  205.  
  206.     UnlockPixels(hPixMap);
  207.     
  208.     bMap->width     = mapSizeX;
  209.     bMap->height    = mapSizeY;
  210.     bMap->rowBytes     = bMap->width * 4;
  211.     bMap->pixelSize = 32;
  212.     bMap->pixelType    = kQ3PixelTypeRGB32;
  213.     bMap->bitOrder    = kQ3EndianBig;
  214.     bMap->byteOrder    = kQ3EndianBig;
  215.     
  216.     /* Free junk */
  217. bail:
  218.  
  219.     SetGWorld(oldGW, oldGD);
  220.     if (pGWorld != NULL)
  221.         DisposeGWorld(pGWorld);
  222.     
  223.     if (textureMapAddr != NULL)
  224.         free(textureMapAddr);
  225.     
  226.     return success;
  227. }
  228.